#include <iostream>
#include <sstream>
#include <map>
#include <string>

using namespace std;

struct Contact {
    string name;
    string phoneNumber;
};

void addContact(map<string, Contact>& addressBook, const string& name, const string& number) {
    addressBook[name] = {name, number};
    cout << "Contact added." << endl;
}

void removeContact(map<string, Contact>& addressBook, const string& name) {
    auto it = addressBook.find(name);
    if (it != addressBook.end()) {
        addressBook.erase(it);
        cout << "Contact removed." << endl;
    } else {
        cout << "Contact not found." << endl;
    }
}

void updateContact(map<string, Contact>& addressBook, const string& name, const string& newNumber) {
    auto it = addressBook.find(name);
    if (it != addressBook.end()) {
        it->second.phoneNumber = newNumber;
        cout << "Contact updated." << endl;
    } else {
        cout << "Contact not found." << endl;
    }
}

void findContact(const map<string, Contact>& addressBook, const string& name) {
    auto it = addressBook.find(name);
    if (it != addressBook.end()) {
        cout << "Contact " << it->second.name << ": " << it->second.phoneNumber << endl;
    } else {
        cout << "Contact not found." << endl;
    }
}

void displayContacts(const map<string, Contact>& addressBook) {
    if (addressBook.empty()) {
        cout << "Address book is empty." << endl;
    } else {
        cout << "Address Book:" << endl;
        for (const auto& pair : addressBook) {
            cout << pair.second.name << ": " << pair.second.phoneNumber << endl;
        }
    }
}

int main() {
    map<string, Contact> addressBook;
    string action;
    string name;
    string number;

    while (true) {
        cout<<"*****************************\n";
        cout << "Enter action \n1.add\n2.remove\n3.update\n4.find\n5.display\n6.exit\n";
        cout<<"*****************************\nplease input here ->";
        cin >> action;

        if (action == "exit") {
            break;
        } else if (action == "add") {
            cout << "Enter name: ";
            cin>>name;
            cout << "Enter number: ";
            cin>>number;
            while(number.size()!=11)
            {
                cout<<"illegal phone number, please retry";
                cin>>number;
            }
            addContact(addressBook, name, number);
        } else if (action == "remove") {
            cout << "Enter name: ";
            cin >> name;
            removeContact(addressBook, name);
        } else if (action == "update") {
            cout << "Enter name: ";
            cin>>name;
            cout << "Enter new number: ";
            cin>>number;
            while(number.size()!=11)
            {
                cout<<"illegal phone number, please retry";
                cin>>number;
            }
            updateContact(addressBook, name, number);
        } else if (action == "find") {
            cout << "Enter name: ";
            cin >> name;
            findContact(addressBook, name);
        } else if (action == "display") {
            displayContacts(addressBook);
        } else {
            cout << "Invalid action." << endl;
        }
        system("pause");
        system("cls");
    }

    return 0;
}